package com.gxuwz.manager.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.gxuwz.manager.dao.LoginDao;
import com.gxuwz.manager.model.Login;
import com.gxuwz.manager.util.DB;

public class LoginServlet extends HttpServlet{
	private static final long serialVersionUID = -5870852067427524781L;
	
	@Override
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		super.doGet(request, response);
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DB db = new DB();
		String name = request.getParameter("userName");
		String password = request.getParameter("password");
		int type = Integer.parseInt(request.getParameter("type"));
		LoginDao loginDao = new LoginDao();
		switch (type) {
			case 1:{
				Login login = loginDao.login(name, password);
				loginDao.closeCon();
				if(login == null){
					response.getWriter().write("loginError");
					return;
				}
				HttpSession session = request.getSession();
				session.setAttribute("user", login);
				session.setAttribute("userType", type);
				request.getRequestDispatcher("otherPeople.jsp").forward(request, response);
				break;
			}
			case 2:{
				Login login = loginDao.login(name, password);
				loginDao.closeCon();
				if(login == null){
					response.getWriter().write("loginError");
					return;
				}
				HttpSession session = request.getSession();
				session.setAttribute("user", login);
				session.setAttribute("userType", type);
				request.getRequestDispatcher("laboratoryManager.jsp").forward(request, response);
				break;
			}
			case 3:{
				Login login = loginDao.login(name, password);
				loginDao.closeCon();
				if(login == null){
					response.getWriter().write("loginError");
					return;
				}
				HttpSession session = request.getSession();
				session.setAttribute("user", login);
				session.setAttribute("userType", type);
				request.getRequestDispatcher("equipmentManager.jsp").forward(request, response);
				break;
			}
			default:
				break;
			}
	}

}
